package cn.legend.travel.admin.user.service.impl;

import cn.legend.travel.admin.user.dao.cache.IUserCacheRepository;
import cn.legend.travel.admin.user.dao.persist.repository.IChatRepository;
import cn.legend.travel.admin.user.dao.persist.repository.IUserRepository;
import cn.legend.travel.admin.user.pojo.entity.User;
import cn.legend.travel.admin.user.pojo.param.UserUpdateParam;
import cn.legend.travel.admin.user.pojo.vo.UserCheckVO;
import cn.legend.travel.admin.user.service.IUserService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements IUserService {
    @Value("${travel.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    private final IUserRepository userRepository;
    private final IUserCacheRepository userCacheRepository;
    private final IChatRepository chatRepository;

    @Override
    public PageData<UserCheckVO> getAllUsers(Integer pageNum) {
        PageData<UserCheckVO> userPage = userRepository.getAllUsers(pageNum, defaultQueryPageSize);
        userPage.getList().forEach(userCheckVO -> userCheckVO.setWarningTimes(userCacheRepository.getWarningTimes(userCheckVO.getId())));
        return userPage;
    }

    @Override
    public PageData<UserCheckVO> getUsersForCheck(Integer pageNum) {
        PageData<UserCheckVO> userPage = userRepository.getUsersForCheck(pageNum, defaultQueryPageSize);
        userPage.getList().forEach(userCheckVO -> userCheckVO.setWarningTimes(userCacheRepository.getWarningTimes(userCheckVO.getId())));
        return userPage;
    }

    @Override
    public void update(UserUpdateParam param) {
        User user = userRepository.getById(param.getId());
        param.setUsername(user.getUsername());

        if (checkWarn(param) && userCacheRepository.shouldForbidden(param.getId(), param.getState())) {
            param.setState(0);
        }
        int row = userRepository.update(new User(param));
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE, "服务器忙,请稍后再试");
        }

        String message = param.getWarningMessage();
        if (message != null) {
            row = chatRepository.sendWarnings(param.getId(), message);
            if (row != 1) {
                throw new ServiceException(ServiceCode.ERROR_INSERT, "服务器忙,请稍后再试");
            }
        }
    }

    @Override
    public void unfreeze(Long id) {
        userCacheRepository.unfreeze(id);
        userRepository.unfreeze(id);
    }

    @Override
    public UserCheckVO getByUsername(String username) {
        UserCheckVO user = userRepository.getByUsername(username);
        user.setWarningTimes(userCacheRepository.getWarningTimes(user.getId()));
        return user;
    }

    @Override
    public UserCheckVO getByPhone(String phone) {
        UserCheckVO user = userRepository.getByPhone(phone);
        user.setWarningTimes(userCacheRepository.getWarningTimes(user.getId()));
        return user;
    }

    private boolean checkWarn(UserUpdateParam param) {
        return param.getDeleteBrief() == 1 || param.getDeleteNickname() == 1 || param.getDeleteAvatar() == 1;
    }
}
